<?php
class Notifikasi extends Controller {

	function Notifikasi(){
		parent::Controller();	
	}
	function index(){
		if(!$this->session->userdata('isLogin')){
			redirect('/home/');
		}else{
			$this->load->model('UsersDetail');
			$this->load->model('UserMahasiswa');
			$this->load->model('User');
			$this->load->model('Notif');
			$this->load->model('Posts');
			$data['nama'] = json_decode($this->UserMahasiswa->getNama($this->session->userdata('userId')))->nama;
			$data ['pageTitle'] = 'Notifikasi | '.$data['nama'];
			if ($allNotifMentionedUnread = $this->Notif->getAllNotifMentionedUnread($this->session->userdata('userId'))){
				for($i=0;$i<sizeOf($allNotifMentionedUnread['post_id']);$i++){
					$mentionedUnread_post_id[$i] =  $allNotifMentionedUnread ['post_id'] [$i];
					$mentionedUnread_mentioner_id[$i] =  $allNotifMentionedUnread ['mentioner_id'] [$i];
					$mentionedUnread_mentioner_username[$i] =  $this->User->getUsername($mentionedUnread_mentioner_id[$i]);
					$mentionedUnread_mentioner_name[$i] = json_decode($this->UserMahasiswa->getNama($mentionedUnread_mentioner_id[$i]))->nama;
				}
				$data['mentionedUnread_mentioner_name'] = $mentionedUnread_mentioner_name;
				$data['mentionedUnread_mentioner_username'] = $mentionedUnread_mentioner_username;
				$data['mentionedUnread_post_id'] = $mentionedUnread_post_id;
				$data['mentionedUnread_id'] = $allNotifMentionedUnread['id'];
			}
			$data['allNotifMentionedUnread'] = $allNotifMentionedUnread;
		
			if ($allNotifMentionedRead = $this->Notif->getAllNotifMentionedRead($this->session->userdata('userId'))){
				for($i=0;$i<sizeOf($allNotifMentionedRead['post_id']);$i++){
					$mentionedRead_post_id[$i] =  $allNotifMentionedRead ['post_id'] [$i];
					$mentionedRead_mentioner_id[$i] =  $allNotifMentionedRead ['mentioner_id'] [$i];
					$mentionedRead_mentioner_username[$i] =  $this->User->getUsername($mentionedRead_mentioner_id[$i]);
					$mentionedRead_mentioner_name[$i] = json_decode($this->UserMahasiswa->getNama($mentionedRead_mentioner_id[$i]))->nama;
				}
				$data['mentionedRead_mentioner_name'] = $mentionedRead_mentioner_name;
				$data['mentionedRead_mentioner_username'] = $mentionedRead_mentioner_username;
				$data['mentionedRead_post_id'] = $mentionedRead_post_id;
			}
			$data['allNotifMentionedRead'] = $allNotifMentionedRead;
			
			if ($allNotifCommented = $this->Notif->getAllNotifCommented($this->session->userdata('userId'))){
				for($i=0;$i<sizeOf($allNotifCommented['post_id']);$i++){
					$commented_post_id[$i] =  $allNotifCommented ['post_id'] [$i];
					$commented_commenter_id[$i] =  $allNotifCommented ['commenter_id'] [$i];
					$commented_commenter_username[$i] =  $this->User->getUsername($commented_commenter_id[$i]);
					$commented_commenter_name[$i] = json_decode($this->UserMahasiswa->getNama($commented_commenter_id[$i]))->nama;
					$commented_read[$i] = $allNotifCommented ['finish'] [$i];
				}
				$data['commented_commenter_name'] = $commented_commenter_name;
				$data['commented_commenter_username'] = $commented_commenter_username;
				$data['commented_post_id'] = $commented_post_id;
				$data['commented_id'] = $allNotifCommented['id'];
				$data['read'] = $commented_read;
			}
			$data['allNotifCommented'] = $allNotifCommented;
			
			if ($allNotifFriend = $this->Notif->getAllNotifFriend($this->session->userdata('userId'))){
				for($i=0;$i<sizeOf($allNotifFriend['id']);$i++){
					$friend_req_id [$i] = $allNotifFriend['id'][$i];
					$friend_requester_id[$i] =  $allNotifFriend ['friend_id'] [$i];
					$friend_requester_name[$i] = json_decode($this->UserMahasiswa->getNama($allNotifFriend ['friend_id'] [$i]))->nama; 
					$friend_requester_username[$i] = $this->User->getUsername($allNotifFriend ['friend_id'] [$i]);
				}
				$data['friend_requester_id'] = $friend_requester_id;
				$data['friend_requester_name'] = $friend_requester_name;
				$data['friend_requester_username'] = $friend_requester_username;
				$data['friend_req_id'] = $friend_req_id;
			}
			$data['allNotifFriend'] = $allNotifFriend;
			
			
			$this->load->view('notifikasi',$data);
		}
		
	}
}